<template>
  <div class="login-register">
    <div class="contain">
      <div class="big-box" :class="{active:isLogin}">
        <div class="big-contain" v-if="isLogin">
          <div class="btitle">账户登录</div>
            <el-form :model="form" ref="form" label-width="100px" :rules="rules" class="bform">
              <el-form-item prop="username" label="用户名">
                <el-input v-model="form.username" placeholder="用户名"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" show-password placeholder="密码"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button @click="login('form')" class="bbutton">登录</el-button>
              </el-form-item>
            </el-form>
        </div>
        <div class="big-contain" v-else>
          <div class="btitle">创建账户</div>
            <el-form :model="form" ref="form" label-width="100px" :rules="rules" class="bform">
              <el-form-item prop="username" label="用户名">
                <el-input v-model="form.username" placeholder="用户名"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" show-password placeholder="密码"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button @click="register('form')" class="bbutton">注册</el-button>
              </el-form-item>
            </el-form>
        </div>
      </div>
      <div class="small-box" :class="{active:isLogin}">
        <div class="small-contain" v-if="isLogin">
          <div class="stitle">你好，朋友!</div>
          <p class="scontent">开始注册，为疫情贡献一份力</p>
          <button class="sbutton" @click="changeType">注册</button>
        </div>
        <div class="small-contain" v-else>
          <div class="stitle">欢迎回来!</div>
          <p class="scontent">与我们保持联系，请登录你的账户</p>
          <button class="sbutton" @click="changeType">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        isLogin: true,
        form: {
          username: 'ldw',
          password: '123'
        },
        rules: {
          username: [{required: true, message: "请输入用户名", trigger: 'blur'}, {min: 3, message: "用户名长度不能小于3位"}],
          password: [{required: true, message: "请输入密码", trigger: 'blur'}]
        }
      };
    },
    methods: {
      login(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.dispatch('login', this.form);
            localStorage.setItem('create', 1)
            setTimeout(() => {
              this.$router.push('/main')
            }, 500)
          } else {
            this.$message({
              type: 'error',
              message: '用户名或密码不符合规范'
            })
          }
        });
      },
      register(formName) {
        this.$refs[formName].validate((valid) =>{
          if(valid) {
            this.$store.dispatch("register", this.form)
            this.changeType()
          } else {
            this.$message({
              type:'error',
              message: '用户名或密码不符合规范'
            })
          }
        });
      },
      changeType() {
        this.isLogin = !this.isLogin
      }
    }
  }
</script>

<style lang="less" scoped>
.login-register {
  width: 100vw;
  height: 100vh;
  box-sizing: border-box;
}

.contain {
  width: 60%;
  height: 60%;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 20px;
  box-shadow: 0 0 3px #f0f0f0,
  0 0 6px #f0f0f0;
}
.big-box {
  width: 70%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 30%;
  transform: translateX(0%);
  transition: all 1s;
}

.big-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.btitle {
  font-size: 1.5em;
  font-weight: bold;
  color: rgb(57, 167, 176);
}

.bform {
  width: 100%;
  height: 50%;
  padding: 2em 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}

.bbutton {
  width: 100%;
  height: 40px;
  border-radius: 24px;
  border: none;
  outline: none;
  background-color: rgb(57, 167, 176);
  color: #fff;
  font-size: 1.0em;
  cursor: pointer;
}
.small-box {
  width: 30%;
  height: 100%;
  background: linear-gradient(135deg, rgb(57, 167, 176), rgb(56, 183, 145));
  position: absolute;
  top: 0;
  left: 0;
  transform: translateX(0%);
  transition: all 1s;
  border-top-left-radius: inherit;
  border-bottom-left-radius: inherit;
}

.small-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.stitle {
  font-size: 1.5em;
  font-weight: bold;
  color: #fff;
}

.scontent {
  font-size: 0.8em;
  color: #fff;
  text-align: center;
  padding: 2em 4em;
  line-height: 1.7em;
}

.sbutton {
  width: 60%;
  height: 40px;
  border-radius: 24px;
  border: 1px solid #fff;
  outline: none;
  background-color: transparent;
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}

.big-box.active {
  left: 0;
  transition: all 0.5s;
}

.small-box.active {
  left: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-top-right-radius: inherit;
  border-bottom-right-radius: inherit;
  transform: translateX(-100%);
  transition: all 1s;
}
</style>
